home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2001 September / PC-WELT 9-2001.ISO / software / hw / brennen / flask_src.exe / Input / IFOParser / Byteswap.h next >
Encoding:
C/C++ Source or Header  |  2000-05-06  |  3.2 KB  |  92 lines

  1. /* Macros to swap the order of bytes in integer values.
  2.    Copyright (C) 1997, 1998 Free Software Foundation, Inc.
  3.    This file is part of the GNU C Library.
  4.  
  5.    The GNU C Library is free software; you can redistribute it and/or
  6.    modify it under the terms of the GNU Library General Public License as
  7.    published by the Free Software Foundation; either version 2 of the
  8.    License, or (at your option) any later version.
  9.  
  10.    The GNU C Library is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.    Library General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU Library General Public
  16.    License along with the GNU C Library; see the file COPYING.LIB.  If not,
  17.    write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  18.    Boston, MA 02111-1307, USA.  */
  19.  
  20. /* Swap bytes in 16 bit value.  */
  21. #define __bswap_constant_16(x) \
  22.      ((((x) >> 8) & 0xff) | (((x) & 0xff) << 8))
  23.  
  24. #if defined __GNUC__ && __GNUC__ >= 2
  25. # define __bswap_16(x) \
  26.      (__extension__                                  \
  27.       ({ register unsigned short int __v;                      \
  28.      if (__builtin_constant_p (x))                          \
  29.        __v = __bswap_constant_16 (x);                      \
  30.      else                                      \
  31.        __asm__ __volatile__ ("rorw $8, %w0"                      \
  32.                  : "=r" (__v)                      \
  33.                  : "0" ((unsigned short int) (x))          \
  34.                  : "cc");                      \
  35.      __v; }))
  36. #else
  37. /* This is better than nothing.  */
  38. # define bswap_16(x) __bswap_constant_16 (x)
  39. #endif
  40.  
  41.  
  42. /* Swap bytes in 32 bit value.  */
  43. #define __bswap_constant_32(x) \
  44.      ((((x) & 0xff000000) >> 24) | (((x) & 0x00ff0000) >>  8) |              \
  45.       (((x) & 0x0000ff00) <<  8) | (((x) & 0x000000ff) << 24))
  46.  
  47. #if defined __GNUC__ && __GNUC__ >= 2
  48. /* To swap the bytes in a word the i486 processors and up provide the
  49.    `bswap' opcode.  On i386 we have to use three instructions.  */
  50. # if !defined __i486__ && !defined __pentium__ && !defined __pentiumpro__
  51. #  define __bswap_32(x) \
  52.      (__extension__                                  \
  53.       ({ register unsigned int __v;                          \
  54.      if (__builtin_constant_p (x))                          \
  55.        __v = __bswap_constant_32 (x);                      \
  56.      else                                      \
  57.        __asm__ __volatile__ ("rorw $8, %w0;"                  \
  58.                  "rorl $16, %0;"                  \
  59.                  "rorw $8, %w0"                      \
  60.                  : "=r" (__v)                      \
  61.                  : "0" ((unsigned int) (x))              \
  62.                  : "cc");                      \
  63.      __v; }))
  64. # else
  65. #  define __bswap_32(x) \
  66.      (__extension__                                  \
  67.       ({ register unsigned int __v;                          \
  68.      if (__builtin_constant_p (x))                          \
  69.        __v = __bswap_constant_32 (x);                      \
  70.      else                                      \
  71.        __asm__ __volatile__ ("bswap %0"                      \
  72.                  : "=r" (__v)                      \
  73.                  : "0" ((unsigned int) (x)));              \
  74.      __v; }))
  75. # endif
  76. #else
  77. # define bswap_32(x) __bswap_constant_32 (x)
  78. #endif
  79.  
  80.  
  81. #if defined __GNUC__ && __GNUC__ >= 2
  82. /* Swap bytes in 64 bit value.  */
  83. # define __bswap_64(x) \
  84.      (__extension__                                  \
  85.       ({ union { __extension__ unsigned long long int __ll;              \
  86.          unsigned long int __l[2]; } __w, __r;                  \
  87.      __w.__ll = (x);                              \
  88.      __r.__l[0] = __bswap_32 (__w.__l[1]);                      \
  89.      __r.__l[1] = __bswap_32 (__w.__l[0]);                      \
  90.      __r.__ll; }))
  91. #endif
  92.